package com.luoc.base.wait;

import lombok.SneakyThrows;

/**
 *  证明 notify 不会释放锁
 */
public class notifyThread {


    class n1{
        public void notifyThread(String lock) throws InterruptedException {
            synchronized (lock){
                System.out.println("notify时间开始: ---------:"+ System.currentTimeMillis());
                lock.notify();
                Thread.sleep(3000);
                System.out.println("notify时间结束: ---------:"+ System.currentTimeMillis());
            }
        }
    }

    class w1{
        public void waitThread(String lock){
             synchronized (lock){
                 System.out.println("wait时间开始: ---------:"+ System.currentTimeMillis());
                 try {
                     lock.wait();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 System.out.println("wait时间结束: ---------:"+ System.currentTimeMillis());
             }
        }
    }


    class myThread1 extends Thread{

        private String lock;
        public myThread1(String lock) {
            this.lock = lock;
        }

        @SneakyThrows
        @Override
        public void run() {
            n1  a =  new notifyThread().new n1();
            a.notifyThread(lock);
        }
    }


    class myThread2 extends Thread {

        private String lock;
        public myThread2(String lock) {
            this.lock = lock;
        }

        @Override
        public void run() {
            w1  a =  new notifyThread().new w1();
            a.waitThread(lock);
        }
    }

    public static void main(String[] args) {

        String a = "lock";
        myThread1 m1 = new notifyThread().new myThread1(a);
        myThread2 m2 = new notifyThread().new myThread2(a);
        myThread2 m3 = new notifyThread().new myThread2(a);

        m2.start();
        m3.start();
        m1.start();

    }
}
